import re

import aiohttp
from bs4 import BeautifulSoup
from fastapi import APIRouter
from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.tools.myparsel import MParsel
from re_common.baselibrary.utils.core.mlamada import bools_string

from apps.allsubdb.cnkicstad.models import DataListModel
from apps.core.return_info import InputInfoModel, ReturnInfo
from apps.crawler_platform.util.requestapihelper import RequestApiHelper

router = APIRouter()


# response_model_exclude_unset 这些默认值将不包括在响应中，仅包含实际设置的值


@router.post("/step1/cnkicgyearlist")
async def down_cnkicgyearlist(cnkicg: InputInfoModel[DataListModel]):
    """
    下载cnki成果分类
    :param cnkicg:
    :return:
    """
    return_info = ReturnInfo()
    mc = MParsel()
    # 获取大分类的ID
    all_dict = dict()
    url = 'https://kns.cnki.net/kns/brief/result.aspx?dbprefix=SNAD'
    marks = cnkicg.data.marks
    proxy = cnkicg.proxy
    sn = aiohttp.ClientSession()
    rrq = RequestApiHelper.get_rrq()
    kwargs = {
        "rrq": rrq,
        "url": url,
        "timeout": 30,
        "marks": marks,
        "sn": sn,
        "proxy": proxy,
        "moths": MRequest.GET,
        "middler_list": [rrq.status_code_middlerwares, rrq.marks_middlerwares]
    }
    bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
    # rrq = AioHttpRequest()
    # rrq.set_sn(sn).set_url(url).set_timeout(30).set_marks(marks).set_proxy(proxy).set_middler_list(
    #     [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    # bools, dicts = await rrq.run(MRequest.GET)
    if bools:
        xpath_dicts = {"div_menu": "//div[@id='XuekeNavi_Div']/div[contains(@class,'leftlist_1')]/@id"}
        bools, id_dict = mc.xpath_parsel_html(rrq.html, xpath_selector=xpath_dicts)
        if bools:
            for v in id_dict.values():
                for NaviCode in v:
                    # 根据成果根分类下载子分类页面
                    url = 'https://kns.cnki.net/KNS/request/NaviGroup.aspx'
                    url += '?code=' + NaviCode
                    url += '&tpinavigroup=SNADtpiresult&catalogName=ZJCLS'
                    rrq.set_marks(['hidden']).set_url(url)
                    bools, dicts = await rrq.run(MRequest.GET)
                    if bools:
                        soup = BeautifulSoup(rrq.html, "lxml")
                        aLinks = soup.find_all('a')
                        dict_ = dict()
                        for a in aLinks:
                            node = a.get('onclick')
                            m = re.match(r"ClickNode\('(\S+)','(\S+)'\);", node)
                            if m:
                                subid = m.groups()[0]
                                subname = "".join(a.stripped_strings)
                                dict_[subid] = subname
                        all_dict[NaviCode] = dict_
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html":all_dict}
    return return_info.todict()


@router.post("/step1_class/wanfangallclasscode")
async def down_wanfangqklist(proxy: str = None):
    """
    /step1_class/wanfangqklist 需要通过该url获取参数
    :param proxy:
    :return:
    """
    pass


@router.post("/step1_class/wanfangqklist")
async def down_wanfangqklist(class_code: str, pageNum: int, proxy: str = None):
    """
    下载分类的期刊列表 从而解析出期刊信息
    :param class_code:
    :param pageNum:
    :param proxy:
    :return:
    """
    pass


@router.post("/step2/down_information")
async def down_wanfangqklist(pykm: str, proxy: str = None):
    """
    下载期刊的元数据信息，为期刊信息服务
    :param pykm:
    :param proxy:
    :return:
    """
    pass


@router.post("/step2/down_synopsis")
async def down_synopsis(pykm: str, proxy: str = None):
    """
    下载期刊的元数据信息，为期刊信息服务
    :param pykm:
    :param proxy:
    :return:
    """
    pass


@router.post("/step5/down_issue_json")
async def down_synopsis(pykm: str, proxy: str = None):
    """
    获取年 和期的json信息
    :param pykm:
    :param proxy:
    :return:
    """


@router.post("/step7/get_article")
async def down_synopsis(page: str, num: str, pubyear: str, pykm: str, proxy: str = None):
    """
    下载某一期 得到文章列表
    :param page:
    :param num:
    :param pubyear:
    :param pykm:
    :param proxy:
    :return:
    """
